package com.comusee.action;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.comusee.service.UserService;

@Controller
@Scope("prototype")
@RequestMapping("user")
public class LoginAction {
	@Resource private UserService userService;

	@RequestMapping(value="/loginui")
	public String loginUI(){
		return "/user/login";
	}
	
	@RequestMapping(value="/login")
	public String login(@RequestParam("username") String username, @RequestParam("password") String password, 
			HttpServletRequest request, HttpSession session){
		System.out.println(username+"-----------"+password);
		
		//验证密码
		if(username!=null && username.length()>1 && password!=null && password.length()>1){
			List<Map<String,Object>> userlist = userService.getUserByUsername(username);
			if(userlist.size()>0){
				String password_c = userlist.get(0).get("password").toString();
				String username_c = userlist.get(0).get("username").toString();
				String realname = userlist.get(0).get("realname").toString();
				
				if(password_c.equals(password)){
					//输入用户名，密码正确
					session.setAttribute("username", username_c);
					session.setAttribute("realname", realname);
					
					return "redirect:/main/mframe";
				}
				
			}
		}
        //这里同时可以提示你输入的用户名密码有误等
		return "redirect:/user/loginui";
	}
	
	@RequestMapping(value="/logout")
	public String logout(HttpSession session){
		session.removeAttribute("username");
		session.removeAttribute("realname");
		return "/user/login";
	}	

}
